package com.example.weight.util;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.weight.MainActivity;
import com.example.weight.R;
import com.example.weight.config.Config;
import com.example.weight.model.Station;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.cachapa.expandablelayout.ExpandableLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestAddInput extends AppCompatActivity {

    private ExpandableLayout expandable_1;
    private LinearLayout ll_test;
    private Button ButtonNo1;
    private List<String> names = new ArrayList<>();

    @SuppressLint({"SetTextI18n", "ResourceType"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_add_input);

        expandable_1=findViewById(R.id.expandable_1);
        ll_test = findViewById(R.id.ll_test);
        ButtonNo1=findViewById(R.id.button1);

        //页面返回按钮
        TitleBar titleBar = findViewById(R.id.titleBar);
        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            //操作人——如果还有未操作的，提示用户，直到所有都操作
            public void onLeftClick(View v) {finish();}
            @Override
            public void onTitleClick(View v) {}
            @Override
            public void onRightClick(View v) {}
        });

        WindowManager wManager = (WindowManager) getSystemService(this.WINDOW_SERVICE);
        int width = wManager.getDefaultDisplay().getWidth() * 3 / 10;

        RequestQueue queue = Volley.newRequestQueue(this);
        String url = Config.url + "/user/login0";
        StringRequest setSysNameRequest = new StringRequest(Request.Method.POST, url,
                new com.android.volley.Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            if (!jsonObject.isNull("data")) {
                                JSONObject data = jsonObject.getJSONObject("data");
                                Log.d("data", data.toString());
                                names.add("zys");
                                names.add("syx");
                                names.add("zsr");

                                for (int i = 0; i < names.size(); i++) {
                                    LinearLayout ll = new LinearLayout(TestAddInput.this);
                                    //        ll.setPadding(20, 0, 0, 0);
                                    //该layoutParams为包裹在外层Linearyout父布局的类型。
                                    ViewGroup.MarginLayoutParams margin = new
                                            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                                    //设置margin值
                                    margin.setMargins(0, 5, 0, 0);
                                    //给需要设置的Linearyout重新设置layoutParams属性
                                    ll.setLayoutParams(margin);

                                    TextView tv = new TextView(TestAddInput.this);
                                    tv.setId(i*2);
                                    tv.setText(names.get(i));
                                    tv.setTextColor(Color.parseColor("#000000"));
                                    EditText et = new EditText(TestAddInput.this);
                                    et.setId(i*2+1);
                                    et.setWidth(width);
                                    ll.addView(tv);
                                    ll.addView(et);

                                    ll_test.addView(ll);
                                }
                            } else {
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> map = new HashMap<>();
                map.put("name", "admin");
                map.put("password", "123456");
                return map;
            }
        };
        setSysNameRequest.setRetryPolicy(new DefaultRetryPolicy(10 * 1000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(setSysNameRequest);



    }
    @SuppressLint("ShowToast")
    public void show (View v) {
//        Toast.makeText(TestAddInput.this, "show", Toast.LENGTH_LONG).show();
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < names.size(); i++) {
            TextView tv = findViewById(i*2);
            EditText et = findViewById(i*2+1);
            str.append(tv.getText().toString()).append(et.getText().toString()).append('\n');
        }
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    }
}